package lanou.a36ke_01.adapters.newsadapter;

import android.content.Context;
import android.graphics.Color;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import java.text.SimpleDateFormat;

import lanou.a36ke_01.beanlists.newsbean.NewsBean;
import lanou.a36ke_01.R;
import lanou.a36ke_01.tools.NetTool;


public class NewsAdapter extends BaseAdapter {

    Context mContext;

    NewsBean newsBean;
    private String urlAll = "https://rong.36kr.com/api/mobi/news?pageSize=20&columnId=all&pagingAction=up";

    // Url 是从newsfragment传递过来的值
    // 用来对比网址
    // 网址为 全部 则显示发布人 否则 不显示
    private String url;

    private final SimpleDateFormat mFormat;


    public void setUrl(String url) {
        this.url = url;
        Log.d("NewsAdapter", url);
    }

    public NewsAdapter(Context mContext) {
        this.mContext = mContext;
        mFormat = new SimpleDateFormat("HH:mm");
    }

    public void setNewsBean(NewsBean newsBean) {
        this.newsBean = newsBean;
        notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        return newsBean.getData().getData().size();
    }

    @Override
    public NewsBean.DataBean.DatailerBean getItem(int position) {
        return newsBean.getData().getData().get(position);
    }


    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder = null;
        if (convertView == null) {
            convertView = LayoutInflater.from(mContext).inflate(R.layout.news_item_adapter, parent, false);
            viewHolder = new ViewHolder(convertView);
            convertView.setTag(viewHolder);

        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }
        viewHolder.title.setText(newsBean.getData().getData().get(position).getTitle());
        viewHolder.name.setText(newsBean.getData().getData().get(position).getUser().getName());


        if (urlAll.equals(url)) {
            viewHolder.columnName.setText(newsBean.getData().getData().get(position).getColumnName());
            switch (newsBean.getData().getData().get(position).getColumnName()) {
                case "大公司":
                    viewHolder.columnName.setTextColor(Color.BLUE);
                    break;
                case "全部":
                    viewHolder.columnName.setTextColor(Color.BLACK);
                    break;
                case "早期项目":
                    viewHolder.columnName.setTextColor(Color.GREEN);
                    break;
                case "深度":
                    viewHolder.columnName.setTextColor(Color.RED);
                    break;
                case "研究":
                    viewHolder.columnName.setTextColor(Color.YELLOW);
                    break;
                case "氪TV":
                    viewHolder.columnName.setTextColor(Color.rgb(240, 128, 128));
                    break;
                case "资本":
                    viewHolder.columnName.setTextColor(Color.rgb(0, 0, 139));
                    break;
                case "B轮后":
                    viewHolder.columnName.setTextColor(Color.rgb(187, 255, 255));
                    break;

            }
        } else {
            viewHolder.columnName.setText(null);
        }


        //获取系统时间
        Long now = System.currentTimeMillis();
        //系统时间减去从网络获取的时间
        long time = now - Long.valueOf(newsBean.getData().getData().get(position).getPublishTime());
        if (time > 1000 * 60 * 60) {
            //显示正常时间
            String date = mFormat.format(Long.valueOf(time));
            viewHolder.publicTime.setText(date);
        } else if (time > 1000 * 60) {
            //显示多长时间之前
            String min = String.valueOf((int) time / 1000 / 60);
            viewHolder.publicTime.setText(min + "分钟之前");
        } else {
            //显示多少s之前
            String s = String.valueOf((int) time / 1000);
            viewHolder.publicTime.setText(s + "s之前");
        }
        NetTool netTool = new NetTool();
        netTool.getImage(newsBean.getData().getData().get(position).getFeatureImg(), viewHolder.feature);
        return convertView;

    }

    public class ViewHolder {
        ImageView feature;
        TextView title;
        TextView name;
        TextView publicTime;
        TextView columnName;

        public ViewHolder(View view) {
            feature = (ImageView) view.findViewById(R.id.news_list_feature);
            title = (TextView) view.findViewById(R.id.news_list_title);
            name = (TextView) view.findViewById(R.id.news_list_name);
            publicTime = (TextView) view.findViewById(R.id.news_list_public_time);
            columnName = (TextView) view.findViewById(R.id.news_list_column_name);
        }
    }
}
